<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Locale set the proper language template for
 * the detected browser language configuration
 * @author SeViR CW http://www.sevir.org
 */
class Locale
{
		var $app;
		var $zend_locale;

		/**
		 * Constructor
		 */
		function Locale(){
			$this->app = & get_instance();
			//TODO: Change Zend locale to native detection
			//Configure Zend locale class for to use BROWSER scope
			try{
				require_once 'Zend/Locale.php';
				$this->zend_locale = new Zend_Locale(Zend_Locale::BROWSER);
			}catch(exception $e){
				log_message('debug','Error al cargar Zend Locale: '.$e->getMessage());
			}

			$this->app->config->load('whilewairport',TRUE,FALSE);

			log_message('debug','Locale library initialized via: '.get_class($this->app));
		}

		/**
		 * It detects the browser language configuration using Zend
		 * locale class
		 * @return {string} locale string
		 */
		function _detect()
		{
		    try{
				$return = ($this->app->session->userdata("lang"))?$this->app->session->userdata("lang"):$this->zend_locale->getLanguage();
			}catch(Exception $e){
				$return = 'en';
			}
			return $return;
		}

		/**
		 * Returns the locale string detected
		 * @return {string} locale string
		 */
		 function get(){
			 return $this->_detect();
		 }

		/**
		* Tries to force a template given to the function.
		* @return {string} forced locale template location
		*/
		function force_template($template, $forced_locale){
			$forced_locale = strtoupper($forced_locale);
			$templatepath = $forced_locale.'/'.$template;

			if (file_exists(BASEPATH.'application/views/'.$templatepath.'.php')){
				return $templatepath;
			}else{
				return strtolower($this->app->config->item('def_lang', 'whilewairport')).'/'.$template;
			}
		}

		/**
		*FORMATEADOR DE FECHAS
		*     -- Puede no pasarse el formato:
		*     -- Si se pasa el formato de fecha tiene que ser del tipo
		*     -- [separador]DMY (donde D=dias, M=meses, Y=a￱o completo
		*/
		function date_formater($mysqlstamp, $wantplus=true, $extformat=false)
		{
			$locale = strtoupper($this->_detect());
			$date = ""; $plus = "";
			$initial = mysql_to_unix($mysqlstamp);
			if ( ((mdate("%H", $initial)!="00") || (mdate("%i", $initial)!="00")) && $wantplus ) $plus = " %H:%i";
			switch($locale)
			{
				case 'ES': $format = (($extformat)?$extformat:"%d/%m/%Y").$plus; break;
				case 'EN': $format = (($extformat)?$extformat:"%m/%d/%Y").$plus; break;
				default: $format = (($extformat)?$extformat:"%d/%m/%Y").$plus; break;
			}
			return mdate($format, mysql_to_unix($mysqlstamp));
		}

		/**
		 * Return the proper locale script or css file
		 * @param {string} the file location
		 * @return {string} the correct locale script
		 */
		 function jsorcss($path,$file){
		 	//$locale = strtoupper($this->_detect());
            $locale = strtolower($this->_detect());

			$filepath = $path.$locale.'/'.$file;

			if (file_exists(BASEPATH.'/../'.$filepath)){
				  return $filepath;
			}else{
				  return $path.strtolower($this->app->config->item('def_lang', 'whilewairport')).'/'.$file;
			}
		 }

		 // Returns the proper localized file
		 function setL10n($path,$file)
		 {
			$locale = strtolower($this->_detect());
			$filepath = $path.'/L10n/'.$locale.'/'.$file;
			if (file_exists(BASEPATH.'/../'.$filepath))
			{
				return $filepath;
			}
			else
			{
				return $path.strtolower($this->app->config->item('def_lang', 'whilewairport')).'/'.$file;
			}
		 }
}
/* End of file Locale.php */
/* Location: /system/application/libraries/Locale.php */